package com.stone.framework.task.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.stone.framework.core.util.Beans;
import com.stone.framework.message.task.JobLanuchReqBody;
import com.stone.framework.message.task.JobRegisterResBody;
import com.stone.framework.message.task.JobStopReqBody;
import com.stone.framework.task.client.AbstractClient;
import com.stone.framework.task.client.ClientFactory;
import com.stone.framework.task.entity.JobRegister;
import com.stone.framework.task.mapper.JobRegisterMapper;
import com.stone.framework.task.service.JobService;

@Service
@Transactional(readOnly = true)
public class JobServiceImpl implements JobService {

    // @Autowired
    // private JobOperator jobOperator;
    //
    // @Autowired
    // private JobExplorer jobExplorer;

    @Autowired
    private JobRegisterMapper jobRegisterMapper;

    @Override
    public List<JobRegisterResBody> getNames() {
        List<JobRegister> list = jobRegisterMapper.selectList(null);
        return list.stream().map(entity -> {
            JobRegisterResBody body = new JobRegisterResBody();
            Beans.copyProperties(entity, body);
            return body;
        }).collect(Collectors.toList());
    }

    @Override
    public void start(JobLanuchReqBody reqBody) {
        AbstractClient client = ClientFactory.valueOf(reqBody.getServiceName()).getBean();
        client.start(reqBody.getJobName(), reqBody.getParams());
    }

    @Override
    public void stop(JobStopReqBody reqBody) {
        AbstractClient client = ClientFactory.valueOf(reqBody.getServiceName()).getBean();
        client.stop(reqBody.getJobExecutionId());
    }

    @Override
    public void restart(JobStopReqBody reqBody) {
        AbstractClient client = ClientFactory.valueOf(reqBody.getServiceName()).getBean();
        client.restart(reqBody.getJobExecutionId());
    }

}
